<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>添加用户</title>

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery-2.1.0.min.js"></script>
</head>
<body>
<div class="container" style="width: 400px;">
    <h3 style="text-align: center;">添加用户</h3>
    <div class="form-group">
        <label for="username">登录账号：</label>
        <input type="text" class="form-control" id="username" name="username" placeholder="请输入登录账号"/>
    </div>

    <div class="form-group">
        <label for="name">姓名：</label>
        <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名"/>
    </div>

    <div class="form-group">
        <label for="password">密码：</label>
        <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"/>
    </div>

    <div class="form-group">
        <label for="password2">确认密码：</label>
        <input type="password2" class="form-control" id="password2" name="password" placeholder="请输入密码"/>
    </div>

    <div class="form-group">
        <label>性别：</label>
        <input id="man" type="radio" name="sex" value="男" checked="checked"/>男
        &nbsp;&nbsp;&nbsp;
        <input id="women" type="radio" name="sex" value="女"/>女
    </div>

    <div class="form-group">
        <label for="age">年龄：</label>
        <input type="number" class="form-control" id="age" name="age" placeholder="请输入年龄"/>
    </div>

    <div class="form-group">
        <label for="address">籍贯：</label>
        <select name="address" id="address" class="form-control">
            <option value="北京">北京</option>
            <option value="上海">上海</option>
            <option value="广州">广州</option>
            <option value="深圳">深圳</option>
            <option value="成都">成都</option>
            <option value="杭州">杭州</option>
            <option value="重庆">重庆</option>
            <option value="西安">西安</option>
            <option value="武汉">武汉</option>
            <option value="沧州">沧州</option>
        </select>
    </div>

    <div class="form-group">
        <label for="qq">QQ：</label>
        <input type="text" id="qq" class="form-control" name="qq" placeholder="请输入QQ号码"/>
    </div>

    <div class="form-group">
        <label for="email">Email：</label>
        <input type="text" id="email" class="form-control" name="email" placeholder="请输入邮箱地址"/>
    </div>

    <div class="form-group" id="adminDiv" style="display: none;">
        <label for="email">管理员：</label>
        <input id="admin_yes" type="radio" name="isadmin" value="1"/>是
        &nbsp;&nbsp;&nbsp;
        <input id="admin_no" type="radio" name="isadmin" value="0" checked="checked"/>否
    </div>

    <div class="form-group" style="text-align: center">
        <input id="btn_sub" class="btn btn-primary" type="button" value="提交" onclick="mysub()"/>
        <input id="btn_back" class="btn btn-default" type="button" value="返回" onclick="location.href='list.html'"/>
    </div>

</div>
<script>
    jQuery.getJSON("/user/addList",{},function(date){
        if(date.body == 1){
            jQuery("#adminDiv").show();
        }
    })
    function mysub(){
        let userName = document.querySelector("#username");
        let name = document.querySelector("#name");
        let passWord = document.querySelector("#password");
        let passWord2 = document.querySelector("#password2");
        //let sex = document.querySelector("#sex");
        let age = document.querySelector("#age");
        //let address = document.querySelector("#address");
        let qq = document.querySelector("#qq");
        let email = document.querySelector("#email");

        if(!userName.value.trim()){
            alert("账号不能为空");
            userName.focus;
            return;
        }
        if(!passWord.value.trim()){
            alert("密码不能为空");
            userName.focus;
            return;
        }
        if(!passWord2.value.trim()){
            alert("确认密码不能为空");
            userName.focus;
            return;
        }
        if(!qq.value.trim()){
            alert("为了更好的服务,QQ不能为空");
            userName.focus;
            return;
        }
        jQuery.getJSON("/user/insert",
        {
            "name":name.value.trim(),
            "username":userName.value.trim(),
            "password":passWord.value.trim(),
            "sex":jQuery("input[name = sex]:checked").val(),
            "age":age.value.trim(),
            "address":jQuery("#address").val(),
            "qq":qq.value.trim(),
            "email":email.value.trim(),
            "isadmin":jQuery("input[name=isadmin]:checked").val()
        },
        function(data){
            if(data.status == 200){
                if(data.body == 1){
                    alert("插入成功");
                    location.assign("list.html");
                }else{
                    alert("插入失败,权限不足");
                }
            }else{
                alert("拒绝访问");
            }
        }
        )
    }
</script>
</body>
</html>